/**
 * 签到页面
 * author:zhangjlt
 * date:2017年3月13日11:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");

	var addlabel = {
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			//返回
			$("#return_addlabel").unbind().on("click", function() {
				window.history.back(-1);
			});
			//			$(".addnewlabel").unbind().on("click", function() {
			//			      
			//			});

			$(document).keypress(function(e) {
				var html = "";
				if(e.keyCode == 13) {
					var label = $(".addnewlabel").val();
					if(label && label.length > 0) {
						console.log(label);
						$(".addnewlabel").val("");
						$(".labelalert").css("display", "none");

						html += '<span class="cen"><span class="labeltab">' +
							label +
							'<i class="deletelabel">x</i></span class="fathercont"></span>';
					} else {
						return;
					}
				}
				$("#newlabel").append(html);
				
				$(".deletelabel").unbind().on("click", function() {
					//删除标签
					console.log(123);
					$(this).parents(".cen").remove();
				});
				});
			$("#labelsubmit").unbind().on("click", function() {
				var item = [];
				$(".labeltab").each(function() {

					var str = $(this)[0].firstChild.data;

					item.push(str);

				});
				console.log(item);
				window.localStorage.setItem("label", JSON.stringify(item));
				window.history.back(-1);
			});
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: true,
					from: "left"
				});
			});
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				var nextPageFrom = "left"; //从右边过来
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: nextPageFrom
				});
			});
			Path.listen();
		},

		/**
		 * 页面数据加载
		 */
		loadData: function() {
			var str = window.localStorage.getItem("labelObj");
			if(str && str.length > 0) {
				var strObj = JSON.parse(str);
				console.log(strObj);
				var html = "";
				for(var i = 0; i < strObj.length; i++) {
					var label = strObj[i];
					html += '<span class="cen"><span class="labeltab">' +
						label +
						'<i class="deletelabel">x</i></span class="fathercont"></span>';

				}
				$(".labelalert").addClass("hidden");
				$("#newlabel").append(html);
				$(".deletelabel").unbind().on("click", function() {
					//删除标签
					$(this).parents(".cen").remove();

				});
			}
		},
		/**
		 * seach page init mothed
		 */
		initialize: function() {

			this.loadData();
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = addlabel;
});